package service

import (
	"golang.org/x/crypto/bcrypt"
	"myserver/dao"
	"myserver/model"
	"myserver/param"
	"myserver/tool"
)

var userDao dao.UserDao

type UserService struct {
}

func Init() {
	userDao = dao.UserDao{MyOrm: tool.MyDb}
}
func (s *UserService) QueryUser(id string) model.User {
	user := userDao.QueryUser(id)
	return user
}

func (s *UserService) CreateUser(user model.User) int64 {
	var IsName = userDao.QueryIsByName(user)
	if IsName {
		return 0
	}
	res := userDao.CreateUser(user)
	return res
}

//用户登录
func (s *UserService) Login(name string, password []byte) model.User {

	//1.使用用户名 + 密码 查询用户信息
	var user model.User = userDao.QueryUserByNamePwd(name)
	err := bcrypt.CompareHashAndPassword([]byte(user.Password), password)
	if err != nil {
		user.ID = 0
		return user
	}
	return user
}

func (s *UserService) DeleteUser(user model.User) int64 {
	res := userDao.DeleteUser(user)
	return res
}

func (s *UserService) UpdateUser(user model.User) int64 {
	var res int64 = userDao.UpdateUser(user)
	return res
}

func (s *UserService) QueryUsers(param param.PageParam) param.ResponsePageParam {
	param.Page = param.Limit * (param.Page - 1)
	res := userDao.QueryUsers(param)
	return res
}

func (s *UserService) QueryUserId(name string) uint {
	return userDao.QueryUserId(name)
}
